# -*- coding: utf-8 -*-
__author__ = 'bitfeng'

import re


# 返回list的第一个非空元素，或空元素
class TakeFirstL(object):

    def __call__(self, values):
        for value in values:
            if value is not None and value.strip() != '':
                return value.strip()
        return ''


# 返回list的最后一个非空元素，或空元素
class TakeLastL(object):

    def __call__(self, values):
        for value in values[::-1]:
            if value is not None and value.strip() != '':
                return value.strip()
        return ''


# 返回list的第n个非空元素，或空元素，第一个为第0个
class TakeNumL(object):

    def __init__(self, num=0):
        self.num = num

    def __call__(self, values):
        if len(values) > self.num:
            return values[self.num].strip()
        else:
            return ''


# 将list的元素的strip(sep)并返回，sep默认为空格
class StripBlankL(object):

    def __init__(self, sep=' '):
        self.sep = sep

    def __call__(self, values):
        vlist = []
        for value in values:
            vlist.append(value.strip(self.sep))
        return vlist


# 讲html中text抽取出，即抽取>和<之间的内容
class ExtractTextL(object):

    def __call__(self, values):
        vlist = []
        for value in values:
            pattern = re.compile(u'(?<=>)[\s\S]*?(?=<)')
            vlist.append(''.join(pattern.findall(value.strip())))
        return vlist


# 删除html的tags，即删除<div class="haha">类似html标签
class RemoveTagsL(object):
    def __call__(self, values):
        vlist = []
        for value in values:
            vlist.append(re.sub(u'<[^<>]+?>', '', value))
        return vlist


# 若list则join，若str则直接返回
class JoinL(object):

    def __init__(self, separator=u' '):
        self.separator = separator

    def __call__(self, values):
        return self.separator.join(values) if type(values) == list else values


class HeadTag(object):

    def __init__(self, tag='head:'):
        self.tag = tag

    def __call__(self, values):
        for value in values:
            if value is not None and value.strip() != '':
                return self.tag+value.strip()
        return ''


# 将list的元素的空格替换成指定符号，默认“|”
class ReplaceBlank(object):

    def __init__(self, sep='|'):
        self.sep = sep

    def __call__(self, values):
        vlist = []
        for value in values:
            if value is not None and value.strip() != '':
                vlist.append(re.sub(u'\s+', '|', ''.join(value.strip())))
        return vlist


# class SplitTextToField(object):
#
#     def __init__(self, sep=' '):
#         self.sep = sep
#
#     def __call__(self, value, split, pos='behind'):
#         values = []
#         split_re = '|'.join(split)
#         for sep in split:
#
#             values.append(re.compile(u''))
#         values = re.compile(split).findall

# 处理html的table
class HtmlTableToDict(object):

    def __init__(self, table, rowT=True, head=0, th=False, td=None):
        self.rowT = rowT
        self.has_th = th
        if not self.has_th:
            self.td = td
        else:
            self.td = None
        self.table = table
        self.dict_data = None
        self.list_data = None

    def table_to_dict(self):
        dict_data = {}
        name = None
        for tr in self.table.xpath('./tr'):
            for i, td in enumerate(tr.xpath('./td')):
                if i % 2 == 0:
                    name = td.xpath('./div/text()').extract_first().strip('：')
                else:
                    if name:
                        dict_data[name] = td.xpath('./text()').extract_first().strip()
        self.dict_data = dict_data
        return dict_data


def remove_tags(values):
    vlist = []
    for value in values:
        vlist.append(re.sub(u'<[^<>]+?>', '', value))
    return vlist


def extract_text(values, sep='|'):
    vlist = []
    pattern = re.compile(u'(?<=>)[\s\S]*?(?=<)')
    for value in values:
        vlist.append(re.sub('\|+', '|', sep.join([v.strip() for v in pattern.findall(value.strip())])))
    return vlist


class ParseHtmlTable(object):

    def __init__(self, table):

        for tr in table.xpath('./tr'):
            tdhs = tr.xpath('.//*')

    def parse_tr(self, tr):
        pass

    def colspan(self, td):
        return td.xpath('./@colspan').extract_first()

    def rowspan(self, td):
        return td.xpath('./@colspan').extract_first()

    def rowscolspan(self, td):
        return self.rowscolspan(td), self.colspan(td)

